<?php
declare (strict_types = 1);

namespace app\admin\controller\erp;

use app\admin\controller\AuthController;
use app\admin\model\erp\Vendors;
use app\admin\model\system\SystemMenus;
use bbweb\services\UtilService as Util;
use think\facade\Config;
use think\facade\Db;
use think\Request;

class BasicsUsers extends AuthController
{
    /**
     * 显示列表
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function index(Request $req)
    {
        if ($req->isAjax()) {
            $data = [];
            $page = $req->param('page/d', 1);
            $limit = $req->param('limit/d', 15);
            $query = Vendors::where('type',3);
            // 条件搜索
            $name = $req->param('name');
            if (!is_null($name) && strlen($name)) {
                $query->where('name','like',"%$name%");
            }
            $query->order('id DESC');
            $data['count'] = $query->count('id');
            $data['data'] = $query->page($page,$limit)->select()->toArray();
            $data['code'] = 0;
            $data['msg'] = '';

            $zTypes = Config::get('erp.user_z_types');
            $uSys = Config::get('erp.user_sys');
            foreach ($data['data'] as &$val) {
                $val['z_type'] = $zTypes[$val['ztype']] ? $zTypes[$val['ztype']] : '--/--';
                $val['sys_type'] = $uSys[$val['sys']] ? $uSys[$val['sys']] : '--/--';
            }

            return json($data);
        }
        $sonAuth = SystemMenus::getSonAuth($req->controller(), $req->action(),array_keys($this->auth));

        // 显示页面
        $this->assign('sonAuth', $sonAuth);
        return $this->fetch();
    }

    /**
     * 创建资源.
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function add(Request $req)
    {
        if($req->isPost()) {
            $data = Util::postMore([
                ['name',''],
                ['sys', 1],
                ['type', 3],
                ['ztype', 4],
                ['phone',''],
                ['phone2',''],
                ['bank',''],
                ['bank_code',''],
                ['bank_name',''],
                ['city_id', 0],
                ['city_bid', 0],
                ['city_cid', 0],
                ['detailed_address',''],
                ['link_mans',[]],
                ['description',''],
            ], $req);
            if (!$data['name']) $this->error('请输入客户名称');
            if($data['city_cid']) $data['address'] = Db::name('citys')->where('id',$data['city_cid'])->value('merger_name');
            if(!empty($data['link_mans'])) {
                $k = 0;
                $info = [];
                foreach ($data['link_mans'] as $v) {
                    if(empty($v['name']) && $v['phone']) continue;
                    $info[] = $v;
                    if ($k == 0) {
                        $data['username'] = $v['name'];
                        $data['phone'] = $v['phone'];
                    }
                    if ($k == 1) {
                        $data['phone2'] = $v['phone'];
                    }
                    $k++;
                }
                $data['link_mans'] = $info;
            }
            if($data['phone'] && Vendors::where('phone|phone2',$data['phone'])->find()) {
                $this->error('客户重复或联系人重复');
            }
            if($data['phone2'] && Vendors::where('phone|phone2',$data['phone2'])->find()) {
                $this->error('客户重复或联系人重复');
            }
            $data['create_at'] = time();
            $data['update_at'] = time();
            $res = Vendors::create($data);
            if($res) {
                $this->success('操作成功!');
            } else {
                $this->error('操作失败');
            }
        }
        $citys = Db::name('citys')->where('parent_id','=',0)->field('id,name')->order('id asc')->select();
        $this->assign('citys', $citys);
        return $this->fetch();
    }

    /**
     * 更新资源
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function edit(Request $req)
    {
        $id = intval($req->param('id/d'));
        if(!$id) $this->error('参数错误','', -1);
        $user = Vendors::where('id', $id)->find();
        if(!$user) $this->error('参数错误','', -1);
        if($req->isPost()) {
            $data = [];
            if($req->param('change/d')) {
                $data['status'] = $req->param('status/d');
            } else {
                $data = Util::postMore([
                    ['name',''],
                    ['sys', 1],
                    ['type', 3],
                    ['ztype', 4],
                    ['phone',''],
                    ['phone2',''],
                    ['bank',''],
                    ['bank_code',''],
                    ['bank_name',''],
                    ['city_id', 0],
                    ['city_bid', 0],
                    ['city_cid', 0],
                    ['detailed_address',''],
                    ['link_mans',[]],
                    ['description',''],
                ], $req);
                if (!$data['name']) $this->error('请输入客户名称');
                if ($data['city_cid']) $data['address'] = Db::name('citys')->where('id', $data['city_cid'])->value('merger_name');
                if (!empty($data['link_mans'])) {
                    $k = 0;
                    $info = [];
                    foreach ($data['link_mans'] as $v) {
                        if(empty($v['name']) && $v['phone']) continue;
                        $info[] = $v;
                        if ($k == 0) {
                            $data['username'] = $v['name'];
                            $data['phone'] = $v['phone'];
                        }
                        if ($k == 1) {
                            $data['phone2'] = $v['phone'];
                        }
                        $k++;
                    }
                    $data['link_mans'] = $info;
                }
                if($data['phone'] && Vendors::where('phone|phone2',$data['phone'])->where('id','<>',$id)->find()) {
                    $this->error('客户重复或联系人重复');
                }
                if($data['phone2'] && Vendors::where('phone|phone2',$data['phone2'])->where('id','<>',$id)->find()) {
                    $this->error('客户重复或联系人重复');
                }
            }
            $data['update_at'] = time();
            $res = Vendors::where('id', $id)->update($data);
            if($res) {
                $this->success();
            } else {
                $this->error();
            }
        }
        $citys = Db::name('citys')->where('parent_id','=',0)->field('id,name')->order('id asc')->select();
        $citysB = $user['city_id'] ? Db::name('citys')->where('parent_id','=',$user['city_id'])->field('id,name')->order('id asc')->select() : [];
        $citysC = $user['city_bid'] ? Db::name('citys')->where('parent_id','=',$user['city_bid'])->field('id,name')->order('id asc')->select() : [];
        $this->assign('citys', $citys);
        $this->assign('citysB', $citysB);
        $this->assign('citysC', $citysC);
        $this->assign('user', $user);
        return $this->fetch();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
